home *** CD-ROM | disk | FTP | other *** search
- /*================================================================================
- stringUtilities.c
- version 1.1
-
- Greg Anderson
- 28 June 1991
- greggor@apple.com
-
- Convienient string utility routines. See the macros defined
- in stringUtilities.h, too.
-
- ================================================================================*/
- #include "stringUtilities.h"
- #include <string.h>
-
- #ifndef __TYPES__
- #include <Types.h>
- #endif
-
- /*--------------------------------------------------------------------------------
- Copy a Pascal string into a C string
- --------------------------------------------------------------------------------*/
- pascal void PtoCcpy( char* cstr, Str255 pstr )
- {
- int i = pstr[0];
-
- cstr[i] = 0;
- while( i > 0 )
- cstr[i - 1] = pstr[i--];
- }
-
- /*--------------------------------------------------------------------------------
- Copy a C string into a Pascal string
- --------------------------------------------------------------------------------*/
- pascal void CtoPcpy( Str255 pstr, char* cstr )
- {
- int i = 1;
-
- while( (*cstr) && (i < 255) )
- pstr[i++] = *cstr++;
- pstr[0] = i - 1;
- }
-
- /*--------------------------------------------------------------------------------
- Compare a Pascal string with a C string
- --------------------------------------------------------------------------------*/
- pascal short PtoCcmp( Str255 pstr, char* cstr )
- {
- char tmp[256];
-
- PtoCcpy( tmp, pstr );
- return( strcmp( tmp, cstr ) );
- }
-
- /*--------------------------------------------------------------------------------
- Copy one pascal string into another
- --------------------------------------------------------------------------------*/
- pascal void pstrcpy( Str255 dest, Str255 src )
- {
- unsigned char* dp;
- unsigned char* sp;
- short i;
- short max;
-
- sp = (unsigned char*)src;
- dp = (unsigned char*)dest;
- max = *sp;
-
- for( i = 0; i <= max ; ++i )
- *dp++ = *sp++;
- }
-
- /*--------------------------------------------------------------------------------
- Copy one pascal string onto the end of another
- --------------------------------------------------------------------------------*/
- pascal void pstrcat( Str255 dest, Str255 src )
- {
- unsigned char* dp;
- unsigned char* sp;
- unsigned char* cp;
- short i;
- short max;
-
- sp = (unsigned char*)src;
- dp = (unsigned char*)dest;
- max = *sp;
- /*
- // Adjust past length & chars already in dest
- */
- cp = dp + *dp + 1;
- ++sp;
- /*
- // Determine how many characters we can really copy
- */
- if( max + *dp > 255 )
- max = 255 - *dp;
- *dp += max;
-
- for( i = 0; i < max ; ++i )
- *cp++ = *sp++;
- }
-
- /*--------------------------------------------------------------------------------
- Compare two Pascal strings
- --------------------------------------------------------------------------------*/
- pascal short pstrcmp( unsigned char* a, unsigned char* b )
- {
- short len = ( *a < *b ? *a : *b );
- short lenEqual = ( *a > *b ) - ( *a < *b );
- short equalityTest;
-
- while( len )
- {
- ++a;
- ++b;
- equalityTest = ( *a > *b ) - ( *a < *b );
-
- if( equalityTest )
- return equalityTest;
-
- --len;
- }
-
- return lenEqual;
- }
-
- /*-------------------------------------------------------------------
- 'Partial' string compare - check if s2 appears at the beginning
- of s1.
-
- Identical to 'strncmp(s1,s2,strlen(s2));', with the bonus feature
- of being case-insensitive.
- -------------------------------------------------------------------*/
- pascal short partialstrcmp( register char* s1, register char* s2)
- {
- register char c1,
- c2;
-
- while( c2 = *s2++ )
- {
- c1 = *s1++;
- c1 = ToUpper(c1);
- c2 = ToUpper(c2);
- if( c1 < c2 ) return(-1);
- if( c1 > c2 ) return( 1);
- }
- return(0);
- }
-
- /*-------------------------------------------------------------------
- Convert an ascii number into a string, advance the string ptr
- past the number
- -------------------------------------------------------------------*/
- pascal short ScanNumberInString( char** line )
- {
- short n = 0;
- while( (**line >= '0') && (**line <= '9') )
- {
- n = n * 10 + (**line - '0');
- ++(*line);
- }
- return n;
- }
-
- /*-------------------------------------------------------------------
- Convert an ascii number into a string
- -------------------------------------------------------------------*/
- pascal short NumberInString( char* line )
- {
- return( ScanNumberInString( &line ) );
- }
-
- /*-------------------------------------------------------------------
- Copy characters from **line into the specified pstr
- -------------------------------------------------------------------*/
- pascal void ScanTextInString( char** line, Str255 pstr, Boolean terminateAtSpace )
- {
- short len = 0;
-
- while( (len < 256) && ( (**line > ' ') || ( (**line == ' ') && (terminateAtSpace == false)) ) )
- {
- ++len;
- pstr[len] = **line;
- (*line)++;
- }
- pstr[0] = len;
- }
-
- /*-------------------------------------------------------------------
- Copy the next word from **line into the specified pstr
- -------------------------------------------------------------------*/
- pascal void ScanWordInString( char** line, Str255 pstr )
- {
- ScanTextInString( line, pstr, true );
- }
-
- /*-------------------------------------------------------------------
- Copy the next line from **line into the specified pstr
- -------------------------------------------------------------------*/
- pascal void ScanLineInString( char** line, Str255 pstr )
- {
- ScanTextInString( line, pstr, false );
- }
-